package controllers;

import java.util.Arrays;
import java.util.List;

import play.cache.Cache;
import play.mvc.Controller;
import client.RestException;
import client.YouGoClient;

import com.ineatconseil.yougo.dto.DetailedUserDto;
import com.ineatconseil.yougo.dto.RequestDto;
import com.ineatconseil.yougo.dto.RequestTypeDto;

public abstract class AbstractConroller extends Controller {

	static YouGoClient client = new YouGoClient();

	public static String getFullname() {
		return session.get("fullname");
	}

	public static Long getUserid() {
		return Long.parseLong(session.get("userid"));
	}

	public static List<RequestTypeDto> getRequestTypes() throws RestException {
		if (Cache.get("requestTypes") == null) {
			Cache.add("requestTypes", Arrays.asList(client.getRequestTypeDto()));
		}
		return (List<RequestTypeDto>) Cache.get("requestTypes");
	}

	public static List<RequestDto> getRequests() throws RestException {
		if (Cache.get("requests") == null) {
			Cache.add("requests", Arrays.asList(client.getRequestDtos(getUserid())));
		}
		return (List<RequestDto>) Cache.get("requests");
	}
	
	public static List<DetailedUserDto> getDetailedUsers() throws RestException {
		if (Cache.get("detailedUser") == null) {
			Cache.add("detailedUser", Arrays.asList(client.getDetailedUsers()));
		}
		return (List<DetailedUserDto>) Cache.get("detailedUser");
	}
	
	
	
	
	public static List<RequestTypeDto> getRequestTypeDto() throws RestException {
		 return Arrays.asList(client.getRequestTypeDto());
	}

	public static boolean cleanRequests() {
		Cache.delete("requests");
		return true;
	}
	
	public static boolean cleanDetailledUser() {
		Cache.delete("detailedUser");
		return true;
	}
}
